package com.tjetc.dao.impl;

import com.tjetc.dao.NurseDao;
import com.tjetc.domain.Nurse;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class NurseDaoImpl implements NurseDao {
    @Override
    public Nurse login(String username, String password) throws SQLException {
        return Db.runner0().query("select * from nurse where username = ? and password = ?",new BeanHandler<>(Nurse.class),
                username,password);
    }

    @Override
    public int getCount(String name) throws SQLException {
        return ((Long) Db.runner0().query("select count(id) from nurse where username like ? ",
                new ScalarHandler<>(),"%"+name+"%")).intValue();
    }

    @Override
    public Nurse findByName(String username) throws SQLException {
        return Db.runner0().query("select * from nurse where username = ?",new BeanHandler<>(Nurse.class), username);
    }

    @Override
    public List<Nurse> list(String name, PageInfo<Nurse> pageInfo) throws SQLException {
        return Db.runner0().query("select * from nurse where username like ?  limit ?,? ",
                new BeanListHandler<>(Nurse.class),"%"+name+"%",
                pageInfo.getStartRecord(),pageInfo.getPageSize());
    }

    @Override
    public List<Nurse> queryByDepid(int depid) throws SQLException {
        return Db.runner0().query("select * from nurse where depid = ?",new BeanListHandler<>(Nurse.class),depid);
    }

    @Override
    public void add(Nurse nurse) throws SQLException {
        Db.runner().update(Db.getConnection(),"insert into nurse(username,age,sex,phone,depid,avatar) values(?,?,?,?,?,?)",
                nurse.getUsername(),nurse.getAge(),nurse.getSex(),nurse.getPhone(),nurse.getDepid(),nurse.getAvatar());
    }

    @Override
    public Nurse getById(int id) throws SQLException {
        return Db.runner0().query("select * from nurse where id=?",new BeanHandler<>(Nurse.class),id);
    }

    @Override
    public int update(Nurse nurse) throws SQLException {
        return Db.runner0().update("update nurse set username=?,phone=?,sex=?,age=?,avatar=? where id = ?",
                nurse.getUsername(),nurse.getPhone(),nurse.getSex(),nurse.getAge(),
                nurse.getAvatar(),nurse.getId());
    }

    @Override
    public void del(int id) throws SQLException {
        Db.runner().update(Db.getConnection(),"delete from nurse where id=?",id);
    }

    @Override
    public Nurse getByName(String username) throws SQLException {
        return Db.runner0().query("select * from nurse where username=?",new BeanHandler<>(Nurse.class),username);
    }

    @Override
    public Nurse getpwdById(int id) throws SQLException {
        return Db.runner0().query("select * from nurse where id=?",new BeanHandler<>(Nurse.class),id);
    }

    @Override
    public int updatepwd(Nurse nurse) throws SQLException {
        return Db.runner0().update("update nurse set password =? where id=?",
                nurse.getPassword(),nurse.getId());
    }
}
